package aspirecn.project3.service.impl;

import aspirecn.project3.mapper.DetailPictureAdressMapper;
import aspirecn.project3.mapper.GoodsCategoryMapper;
import aspirecn.project3.mapper.GoodsDetailMapper;
import aspirecn.project3.model.DetailPicAdress;
import aspirecn.project3.model.GoodsCategory;
import aspirecn.project3.model.GoodsDetail;
import aspirecn.project3.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class GoodServiceImpl implements GoodService {
    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Autowired
    GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    private DetailPictureAdressMapper detailPicAdress;

    @Override
    public List<GoodsDetail> getGoodsByPage(int pages, int limit, String key) {
        List<GoodsDetail> goodsList = goodsDetailMapper.getGoodsByPage(key, pages, limit);
        return goodsList;
    }

    @Override
    public int deleteGoodsById(long id) {
        int i2 = detailPicAdress.deleteGoodDetailPic(id);
        int i1 = goodsDetailMapper.deleteGoods(id);

        return 0;
    }

    @Override
    public List<GoodsDetail> getGoodsById(long id) {
        List<GoodsDetail> goods = goodsDetailMapper.getGoodsById(id);
        return goods;
    }

    @Override
    public List<DetailPicAdress> getGoodsPicById(long id) {

        List<DetailPicAdress> detailPicAdressList = detailPicAdress.getGoodsById(id);
        return detailPicAdressList;
    }

    @Override
    public int modifyGoodsPicById(long id, List<String> adresses) {
        detailPicAdress.deleteGoodDetailPic(id);
        for (int i = 0; i < adresses.size(); i++) {
            detailPicAdress.addGoodsDetailPic(id, adresses.get(i));
        }

        return 0;
    }

    @Override
    public int modifyGoodsById(GoodsDetail goodsDetail) {
        int i = goodsDetailMapper.modifyGood(goodsDetail);
        return i;
    }

    @Override
    public int addGoods(GoodsDetail goodsDetail) {
        int i = goodsDetailMapper.addGoods(goodsDetail);
        return i;
    }

    @Override
    public int addGoodsPics(DetailPicAdress adress) {
        int i = detailPicAdress.addGoodsDetailPic(adress.getGoodId(), adress.getPictureAddress());
        return i;
    }

    @Override
    public void saveCategory(GoodsCategory goodsCategory) {
        goodsCategoryMapper.save(goodsCategory);
    }

    @Override
    public List<GoodsCategory> getGoodsCategoryOne() {
        return goodsCategoryMapper.getListOne();
    }

    @Override
    public List<GoodsCategory> getGoodsCategoryByParent(Long id) {
        return goodsCategoryMapper.getListByParent(id);
    }


}
